<?php

/*

Quests (quests.php)

*/

$page_title = "Quests";
$rank_check = 1;
include "header.inc.php";

mysql_query("DELETE FROM gave_up WHERE wait_til <= '$timestamp'");

print "$openHTML";

$findSetup = fetch("SELECT * FROM quest_items WHERE game = '$game'");
if (!$findSetup[id])
{
	if ($rank == 7)
	{
		$ifAdmin = "<br><b>Admins:</b> Click <a href=admin/admin_setup_quests.php?game=$game>here</a> to setup this game!<br>";
	}

	die("This game has not been setup to play yet. Sorry.$ifAdmin$closeHTML");
}

$findGiveUp = fetch("SELECT * FROM gave_up WHERE game = '$game' AND user = '$userid' AND game_name = 'Quests'");
if ($findGiveUp[id])
{
	die("You recently gave up on a quest. You will need to wait a while before you can have another one.$closeHTML");
}

$findQuest = fetch("SELECT * FROM quests WHERE game = '$game' AND user = '$userid'");
if (!$findQuest[id])
{

	$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$pageTitle' AND game = 	'$game'");
	if ($findPage[page_info] == "")
	{

ECHO <<<END

<p>Looking for a quest? Choose a difficulty level below and start questing!! Bring back items and you get more items and points in return!</p>

END;

	}
	else
	{
		include "showpage.inc.php";
		print "$findPage[page_info]";
	}

ECHO <<<HTML

	<CENTER><TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=400>
	   <TR>
	      <TD>
	         <CENTER><TABLE BGCOLOR="#00FF00" CELLSPACING=1 CELLPADDING=0 WIDTH=100 	HEIGHT=25>
	            <TR>
	               <TD BGCOLOR="#CCFFCC">
	                  <CENTER><A HREF="quests.pro.php?act=start&diff=easy&game=$game"><FONT SIZE="-1" 	COLOR="#006600">Easy</FONT></A></CENTER>
	               </TD>
	            </TR>
	         </TABLE>
	         </CENTER>
	      </TD>
	      <TD>
	         <CENTER><TABLE BGCOLOR="#3399FF" CELLSPACING=1 CELLPADDING=0 WIDTH=100 	HEIGHT=25>
	            <TR>
	               <TD BGCOLOR="#99CCFF">
	                  <CENTER><A HREF="quests.pro.php?act=start&diff=med&game=$game"><FONT SIZE="-1" 	COLOR="#000099">Medium</FONT></A></CENTER>
	               </TD>
	            </TR>
	         </TABLE>
	         </CENTER>
	      </TD>
	      <TD>
	         <CENTER><TABLE BGCOLOR="#FF6666" CELLSPACING=1 CELLPADDING=0 WIDTH=100 	HEIGHT=25>
	            <TR>
	               <TD BGCOLOR="#FFCCCC">
	                  <CENTER><A HREF="quests.pro.php?act=start&diff=hard&game=$game"><FONT SIZE="-1" 	COLOR="#990000">Hard</FONT></A></CENTER>
	               </TD>
	            </TR>
         </TABLE>
         </CENTER>
      </TD>
   </TR>
</TABLE>
</CENTER>

HTML;

}

if ($findQuest[id])
{
	$explodeItems = explode(", ", $findQuest[items_needed]);
	$countItems = count($explodeItems) - 1;
	for ($x = 0; $x <= $countItems; $x++)
	{
		$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeItems[$x]' AND game = '$game'");
		$bringItems .= "<td><p align=center><img src=images/user_images/opg_$game/items/item_$findItem[id].gif><br>$findItem[item_name]</p></td>";
		$findYourItem = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1");
		$yourItemID = $findYourItem[id];

		if ((!$findYourItem[id]) OR ($taken[$yourItemID]))
		{
			$findYourItem2 = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1,2");
			if ($findYourItem2[id])
			{
				$yourItemID = $findYourItem2[id];
			}
			if (!$findYourItem2[id])
			{
				$findYourItem3 = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1,3");
				if ($findYourItem3[id])
				{
					$yourItemID = $findYourItem3[id];
				}
				if (!$findYourItem3[id])
				{
					$bringItems2 .= "<td><p align=center><font color=red><b>X</b></font></p></td>";
				}
			}

		}

		if (!$taken[$yourItemID])
		{
			if ($findYourItem[id])
			{
				$bringItems2 .= "<td><p align=center><font 	color=green><b>X</b></font></p></td>";
				$taken[$yourItemID] = $yourItemID;
			}
		}
	}

	if ($timestamp >= $findQuest[end_time])
	{
		mysql_query("DELETE FROM quests WHERE game = '$game' AND user = '$userid'");
		die("<p align=center><b>You have run out of time! <a href=quests.php?game=$game>Try again!</a></b></p>$closeHTML");
	}

	$diff = $findQuest[end_time] - $timestamp;

	$hours = ($diff - ($diff % 3600)) / 3600;
	$diff = $diff - ($hours * 3600);
	$minutes = ($diff - ($diff % 60)) / 60;
	$diff = $diff - ($minutes * 60);
	$seconds = ($diff - ($diff % 1)) / 1;

	if ($hours >= 1) { $timeLeft = "$hours Hours, "; }
	if ($minutes >= 1) { $timeLeft .= "$minutes Minutes, "; }
	if ($seconds) { $timeLeft .= "$seconds Seconds"; }

	print "<p align=center>You have $timeLeft left to complete this quest!</p>";

	print "<p align=center><table><tr>$bringItems</tr><tr>$bringItems2</tr></table>";

	print "<p><form action=quests.pro.php?game=$game&act=end method=post><p align=center><input type=submit value=\"I have the items!\"></p></form></p><p><form action=quests.pro.php?game=$game&act=giveup method=post><p align=center><input type=submit value=\"I give up!\"></p></form></p>";

}

$x = 1;
$findDaily5 = mysql_query("SELECT * FROM highscores WHERE game_name = 'Quests - Daily' AND game= '$game' AND daily = '$datestamp' ORDER BY score DESC LIMIT 5");
while ($getDaily5 = mysql_fetch_array($findDaily5))
{
	$getUsername = fetch("SELECT * FROM members2 WHERE game = '$game' AND id = '$getDaily5[user]'");
	$daily5 .= "$x. <A href=user_profile.php?game=$game&user=$getUsername[username]>$getUsername[display_name]</a> - $getDaily5[score] Quests<br>";
	$x++;
}

$x = 1;
$findTop5 = mysql_query("SELECT * FROM highscores WHERE game_name = 'Quests' AND game= '$game' ORDER BY score DESC LIMIT 5");
while ($getTop5 = mysql_fetch_array($findTop5))
{
	$getUsername = fetch("SELECT username,display_name,premium,display_prem FROM members2 WHERE game = '$game' AND id = '$getTop5[user]'");
	if (($getUsername[premium] == 1) AND ($getUsername[display_prem]))
	{
		$getUsername[display_name] = $getUsername[display_prem];
	}
	$top5 .= "$x. <A href=user_profile.php?game=$game&user=$getUsername[username]>$getUsername[display_name]</a> - $getTop5[score] Quests<br>";
	$x++;
}

$yourDailyScore = fetch("SELECT score FROM highscores WHERE game_name = 'Quests - Daily' AND game= '$game' AND user = '$userid' AND daily = '$datestamp'");
$yourTotalScore = fetch("SELECT score FROM highscores WHERE game_name = 'Quests' AND game= '$game' AND user = '$userid'");

print "<p align=center><TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=400>
   <TR BGCOLOR=\"#F5E462\">
      <TD>
         <CENTER><B>Daily Scores</B></CENTER>
      </TD>
      <TD>
         <CENTER><B>Cumulative Scores</B></CENTER>
      </TD>
   </TR>
   <TR BGCOLOR=\"#FCFFDE\">
      <TD>
         $daily5

         <P>You: $yourDailyScore[score]</P>
      </TD>
      <TD>
         $top5

         <P>You: $yourTotalScore[score]</P>
      </TD>
   </TR>
</TABLE></p>";

print "$closeHTML";

?>